#coding=utf-8
#Boa:Dialog:DlgQuickAddProduct

import wx

from product import CDataProduct,CDataCategory,CDataKeyword,CDataProLabel
from config import CBriefCode

def create(parent):
    return DlgQuickAddProduct(parent)

[wxID_DLGQUICKADDPRODUCT, wxID_DLGQUICKADDPRODUCTBTNADD, 
 wxID_DLGQUICKADDPRODUCTBTNSEARCHCATEGORY, 
 wxID_DLGQUICKADDPRODUCTBTNSEARCHKEYWORD, wxID_DLGQUICKADDPRODUCTLCPROLIST, 
 wxID_DLGQUICKADDPRODUCTTCKEYWORD, wxID_DLGQUICKADDPRODUCTTCNAME, 
 wxID_DLGQUICKADDPRODUCTTCSIMCODE, wxID_DLGQUICKADDPRODUCTTCSPEC, 
 wxID_DLGQUICKADDPRODUCTTCTYPE, wxID_DLGQUICKADDPRODUCTTCUNIT, 
 wxID_DLGQUICKADDPRODUCTTKEYWORD, wxID_DLGQUICKADDPRODUCTTNAME, 
 wxID_DLGQUICKADDPRODUCTTSIMCODE, wxID_DLGQUICKADDPRODUCTTSPEC, 
 wxID_DLGQUICKADDPRODUCTTTYPE, wxID_DLGQUICKADDPRODUCTTUNIT, 
] = [wx.NewId() for _init_ctrls in range(17)]

class DlgQuickAddProduct(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGQUICKADDPRODUCT,
              name=u'DlgQuickAddProduct', parent=prnt, pos=wx.Point(494, 136),
              size=wx.Size(734, 529),
              style=wx.TAB_TRAVERSAL | wx.DEFAULT_DIALOG_STYLE,
              title=u'\u5feb\u901f\u6dfb\u52a0\u4ea7\u54c1')
        self.SetClientSize(wx.Size(726, 495))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.Center(wx.BOTH)

        self.tName = wx.StaticText(id=wxID_DLGQUICKADDPRODUCTTNAME,
              label=u'\u540d\u79f0\uff1a', name=u'tName', parent=self,
              pos=wx.Point(48, 120), size=wx.Size(51, 16), style=0)
        self.tName.SetAutoLayout(False)

        self.tType = wx.StaticText(id=wxID_DLGQUICKADDPRODUCTTTYPE,
              label=u'\u7c7b\u578b\uff1a', name=u'tType', parent=self,
              pos=wx.Point(48, 40), size=wx.Size(64, 14), style=0)

        self.tcType = wx.TextCtrl(id=wxID_DLGQUICKADDPRODUCTTCTYPE,
              name=u'tcType', parent=self, pos=wx.Point(120, 32),
              size=wx.Size(128, 26), style=0,
              value=u'\u9ed8\u8ba4\uff1a\u6240\u6709\u5546\u54c1')
        self.tcType.SetEditable(False)
        self.tcType.Enable(False)

        self.btnSearchCategory = wx.Button(id=wxID_DLGQUICKADDPRODUCTBTNSEARCHCATEGORY,
              label=u'+', name=u'btnSearchCategory', parent=self,
              pos=wx.Point(256, 32), size=wx.Size(25, 25), style=0)
        self.btnSearchCategory.Bind(wx.EVT_BUTTON,
              self.OnBtnSearchCategoryButton,
              id=wxID_DLGQUICKADDPRODUCTBTNSEARCHCATEGORY)

        self.tKeyword = wx.StaticText(id=wxID_DLGQUICKADDPRODUCTTKEYWORD,
              label=u'\u5173\u952e\u5b57\uff1a', name=u'tKeyword', parent=self,
              pos=wx.Point(48, 80), size=wx.Size(68, 16), style=0)

        self.tcKeyword = wx.TextCtrl(id=wxID_DLGQUICKADDPRODUCTTCKEYWORD,
              name=u'tcKeyword', parent=self, pos=wx.Point(120, 72),
              size=wx.Size(416, 26), style=0,
              value=u'\u5546\u54c1\u6807\u7b7e')
        self.tcKeyword.SetEditable(False)
        self.tcKeyword.Enable(False)

        self.btnSearchKeyword = wx.Button(id=wxID_DLGQUICKADDPRODUCTBTNSEARCHKEYWORD,
              label=u'+', name=u'btnSearchKeyword', parent=self,
              pos=wx.Point(544, 72), size=wx.Size(25, 25), style=0)
        self.btnSearchKeyword.Bind(wx.EVT_BUTTON, self.OnBtnSearchKeywordButton,
              id=wxID_DLGQUICKADDPRODUCTBTNSEARCHKEYWORD)

        self.lcProList = wx.ListCtrl(id=wxID_DLGQUICKADDPRODUCTLCPROLIST,
              name=u'lcProList', parent=self, pos=wx.Point(48, 184),
              size=wx.Size(608, 272),
              style=wx.LC_HRULES | wx.LC_VRULES | wx.LC_REPORT)

        self.tUnit = wx.StaticText(id=wxID_DLGQUICKADDPRODUCTTUNIT,
              label=u'\u5355\u4f4d\uff1a', name=u'tUnit', parent=self,
              pos=wx.Point(360, 160), size=wx.Size(51, 16), style=0)

        self.tSpec = wx.StaticText(id=wxID_DLGQUICKADDPRODUCTTSPEC,
              label=u'\u89c4\u683c\uff1a', name=u'tSpec', parent=self,
              pos=wx.Point(48, 160), size=wx.Size(51, 16), style=0)
        self.tSpec.SetAutoLayout(False)

        self.tSimCode = wx.StaticText(id=wxID_DLGQUICKADDPRODUCTTSIMCODE,
              label=u'\u62fc\u97f3\u7801\uff1a', name=u'tSimCode', parent=self,
              pos=wx.Point(360, 120), size=wx.Size(68, 16), style=0)

        self.tcSimCode = wx.TextCtrl(id=wxID_DLGQUICKADDPRODUCTTCSIMCODE,
              name=u'tcSimCode', parent=self, pos=wx.Point(432, 112),
              size=wx.Size(120, 24), style=0, value=u'')
        self.tcSimCode.SetEditable(False)
        self.tcSimCode.Enable(False)

        self.tcName = wx.TextCtrl(id=wxID_DLGQUICKADDPRODUCTTCNAME,
              name=u'tcName', parent=self, pos=wx.Point(120, 112),
              size=wx.Size(200, 26), style=0,
              value=u'\u5fc5\u586b\uff0c\u5141\u8bb8\u91cd\u590d')
        self.tcName.Bind(wx.EVT_TEXT, self.OnTCNameText,
              id=wxID_DLGQUICKADDPRODUCTTCNAME)

        self.tcSpec = wx.TextCtrl(id=wxID_DLGQUICKADDPRODUCTTCSPEC,
              name=u'tcSpec', parent=self, pos=wx.Point(120, 152),
              size=wx.Size(200, 26), style=0, value=u'')
        self.tcSpec.Bind(wx.EVT_TEXT, self.OnTCNameText,
              id=wxID_DLGQUICKADDPRODUCTTCSPEC)

        self.tcUnit = wx.TextCtrl(id=wxID_DLGQUICKADDPRODUCTTCUNIT,
              name=u'tcUnit', parent=self, pos=wx.Point(432, 152),
              size=wx.Size(56, 26), style=0, value=u'\u5377')

        self.btnAdd = wx.Button(id=wxID_DLGQUICKADDPRODUCTBTNADD,
              label=u'\u6dfb\u52a0', name=u'btnAdd', parent=self,
              pos=wx.Point(560, 152), size=wx.Size(93, 26), style=0)
        self.btnAdd.SetDefault()
        self.btnAdd.Bind(wx.EVT_BUTTON, self.OnBtnAddButton,
              id=wxID_DLGQUICKADDPRODUCTBTNADD)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        # 141209 - It seems donot work well.
        #order = (self.tcName, self.tcSpec, self.tcUnit, self.btnAdd, self.tcName)
        #for i in range(len(order) - 1):
            #order[i+1].MoveAfterInTabOrder(order[i])
            
        self.tcName.SetInsertionPointEnd()
        self.tcName.SetSelection(-1, -1)
        self.tcName.SetFocus()
        
        self.curCategory = None
        self.curKeywords = []
        
        cols = [[u'名称', wx.LIST_FORMAT_LEFT, 150],
                [u'规格', wx.LIST_FORMAT_LEFT, 150],
                [u'单位', wx.LIST_FORMAT_LEFT, -1],
                [u'类型', wx.LIST_FORMAT_LEFT, 100]]
        for i in range(len(cols)):
            self.lcProList.InsertColumn(i, cols[i][0], cols[i][1], cols[i][2])        
        
    def OnBtnSearchCategoryButton(self, event):
        import DlgCategory
        dlg = DlgCategory.create(self)
        if (self.curCategory is not None):
            dlg.setSelection(self.curCategory)
        btn = event.GetEventObject()
        btnPos = btn.ClientToScreen( (0,0) )
        btnSize =  btn.GetSize()
        dlgSize = dlg.GetSize()
        dlg.Move((btnPos.x-dlgSize[0]/2, btnPos.y+btnSize[1]))
        if (dlg.ShowModal() == wx.ID_CANCEL):
            return
        
        self.curCategory = dlg.categorySel
        if (self.curCategory is None):
            self.tcType.SetValue('')
        else:
            self.tcType.SetValue(self.curCategory.getFieldVal(index=CDataCategory.COL_NAME))

    def OnBtnSearchKeywordButton(self, event):
        import DlgKeyword
        dlg = DlgKeyword.create(self)
        btn = event.GetEventObject()
        btnPos = btn.ClientToScreen( (0,0) )
        btnSize =  btn.GetSize()
        dlgSize = dlg.GetSize()
        dlg.Move((btnPos.x-dlgSize[0]/2, btnPos.y+btnSize[1]))
        if (dlg.ShowModal() == wx.ID_CANCEL):
            return

        self.curKeywords = dlg.keywordSel
        self.tcKeyword.SetValue(CDataKeyword.parseStr(self.curKeywords))

    def OnTCNameText(self, event):
        
        name = self.tcName.GetValue()
        code = CBriefCode.getBriefCode(name)
        self.tcSimCode.SetValue(code)
        self.tcSpec.SetValue(name)
        
    def insertList(self, proObj):
        import sys
        name = proObj.getValue(proObj.COL_NAME)
        spec = proObj.getValue(proObj.COL_SPEC)
        unit = proObj.getValue(proObj.COL_UNIT1)
        typeId = proObj.getValue(proObj.COL_CATEGORYID)
        type = CDataCategory.getName(typeId)
        
        row = self.lcProList.InsertStringItem(sys.maxint, '')
        self.lcProList.SetStringItem(row, 0, name)
        self.lcProList.SetStringItem(row, 1, spec)
        self.lcProList.SetStringItem(row, 2, unit)
        self.lcProList.SetStringItem(row, 3, type)
            
    def OnBtnAddButton(self, event):
        name = self.tcName.GetValue()
        spec = self.tcSpec.GetValue()
        if (name == ''):
            dlg = wx.MessageDialog(self,
                            u'没有填写商品名称',
                            u'提示', wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return False
    
        briefCode = CBriefCode.getBriefCode(name)
        
        if (self.curCategory is None):
            typeid = CDataCategory.getDefCategoryId()
        else:
            typeid = self.curCategory.getFieldVal(index=CDataCategory.COL_ID)
            
        unit = self.tcUnit.GetValue()

        data = {CDataProduct.COL_NAME:name,
                CDataProduct.COL_SPEC:spec,
                CDataProduct.COL_BRIEFCODE:briefCode,
                CDataProduct.COL_CATEGORYID:typeid,
                CDataProduct.COL_UNIT1:unit,
                CDataProduct.COL_RATIO1:None,
                CDataProduct.COL_ONHAND1:0.0,
                CDataProduct.COL_DEFCOST1:0.0,
                CDataProduct.COL_DEFSALE1:0.0,
                CDataProduct.COL_LASTPUR1:0.0,
                CDataProduct.COL_MINPUR1:0.0,
                CDataProduct.COL_MAXPUR1:0.0}
        
        proObj = CDataProduct.factory()
        proObj.setMulFieldVal(data)
        proObj.insert()
        
        #CDataProLabel.delData({CDataProLabel.COL_PROID:proId})
        for i in range(len(self.curKeywords)):
            obj = CDataProLabel.factory()
            fields = {CDataProLabel.COL_PROID:proObj.getValue(proObj.COL_ID),
                        CDataProLabel.COL_LABELID:self.curKeywords[i].getValue(CDataKeyword.COL_ID)}
            obj.setMulFieldVal(fields)
            obj.insert()
            
        self.insertList(proObj)
        
        self.tcName.SetValue('')
        self.tcName.SetInsertionPointEnd()
        self.tcName.SetSelection(-1, -1)
        self.tcName.SetFocus()
